//
//  XLAllApplyAdapter.m
//  industry
//
//  Created by xl on 2020/11/27.
//  Copyright © 2020 Haier. All rights reserved.
//

#import "XLAllApplyAdapter.h"
#import "XLAllApplyViewProtocol.h"
#import "XLAllApplyVC.h"

@interface XLAllApplyAdapter ()

@end
@implementation XLAllApplyAdapter

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 2;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == 0) {
        return self.commonArr.count;
    }
    return self.allArr.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(58, 83);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return CGSizeMake(SCREEN_WIDTH, 45);
    }
    return self.allArr.count > 0 ? CGSizeMake(SCREEN_WIDTH, 45):CGSizeZero;;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
     return UIEdgeInsetsMake(0, 30, 0, 30);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 10.0f;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return floor((SCREEN_WIDTH - 30*2 - 58*4)/3.0);
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        XLAllApplyHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"XLAllApplyHeaderView" forIndexPath:indexPath];
        if (indexPath.section == 0) {
            WEAKSELF
            headerView.editBlock = ^{
                STRONGSELF
                
                [strongSelf gotoAllApplyVC:strongSelf.commonArr allArr:strongSelf.allArr withCollectionView:collectionView];
//                strongSelf.isEdit = YES;
//                XLSend(collectionView.baseController.cView, XLAllApplyViewProtocol, refreshCurrentViewLayout);
            };
            [headerView loadHeaderViewTitle:@"常用" showEdit:!_isEdit];
        } else {
            [headerView loadHeaderViewTitle:@"全部应用" showEdit:NO];
        }
        return headerView;
    }
    return nil;
}

- (void)gotoAllApplyVC:(NSArray *)commonArr
                allArr:(NSArray *)allArr
    withCollectionView:(UICollectionView *)collView{
    XLAllApplyVC *vc = [[XLAllApplyVC alloc] init];
    vc.commonArr = commonArr;
    vc.allArr = allArr;
    vc.isEdit = YES;
    WEAKSELF
    vc.block = ^(NSArray * _Nonnull commonArr, NSArray * _Nonnull allArr) {
        STRONGSELF
       //传入，新的值，并刷新，
        strongSelf.commonArr = commonArr;
        strongSelf.allArr = allArr;
        [collView reloadData];
       // 更改change状态。
        XLAllApplyVC *vc = (XLAllApplyVC *)strongSelf.currentTopController;
        vc.isChange = YES;
//        ((XLAllApplyVC *)strongSelf.baseController).isChange = YES;
    };
    [self.currentTopController.navigationController pushViewController:vc animated:YES];
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
     XLAllApplyCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"XLAllApplyCell" forIndexPath:indexPath];
    if (indexPath.section == 0) {
        [cell loadCellData:self.commonArr[indexPath.row] isEdit:_isEdit isCommon:YES];
    } else {
        [cell loadCellData:self.allArr[indexPath.row] isEdit:_isEdit isCommon:NO];
    }
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    id cellData;
    if (indexPath.section == 0) {
        cellData = self.commonArr[indexPath.row];
    } else {
        cellData = self.allArr[indexPath.row];
    }
    if (cellData != nil && self.adapterDelegate && [self.adapterDelegate respondsToSelector:@selector(didSelectCellData:)]) {
        [self.adapterDelegate didSelectCellData:cellData];
    }
}
@end
